﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LTM_Extra
{
    public class Variable
    {

        string lableText = string.Empty;

        public string LableText
        {
            get
            {
                if (!string.IsNullOrEmpty(lableText))
                {
                    return lableText;
                }
                else
                {
                    return VariableName;
                }
            }
            set
            {
                lableText = value;
            }
        }

        public string VariableName
        {
            get;
            set;
        }

        public string VariableType { get; set; }
        public string ErrorMessage { get; set; }
        public string ValidationRule { get; set; }
        public string DefaultValue { get; set; }
        public string InitialValue { get; set; }
        public bool Required { get; set; }

        private IList<string> DataSource
        {
            get
            {
                return InitialValue.Split(new string[] { Constants.InitialValueSeperator }, StringSplitOptions.RemoveEmptyEntries).ToList();
            }
        }

        public Label GetLableControl()
        {
            Label lbl = new Label();
            lbl.Name = Constants.LabelPrefix + VariableName;
            lbl.Text = LableText;
            return lbl;

        }

        public Control GetContentControl()
        {

            Control control = null;
            
            string namePrefix = string.Empty;
           
            switch (VariableType)
            {
                case Constants.VariableType.Email:
                case Constants.VariableType.Integer:
                case Constants.VariableType.String:
                case Constants.VariableType.MultilineString:
                    control = new TextBox();
                    control.Text = DefaultValue;
                    namePrefix = Constants.TextBoxPrefix;
                break;

                case Constants.VariableType.Combox:
                    var cmb=new ComboBox();
                    control = cmb;
                    cmb.DropDownStyle = ComboBoxStyle.DropDownList;
                    cmb.DataSource=DataSource;
                    namePrefix = Constants.ComBoxPrefix;
                    break;

                default:
                    break;
            }

            control.Name = namePrefix + VariableName;
            control.Size = new System.Drawing.Size(250, 20);
            if (VariableType == Constants.VariableType.MultilineString)
            {
                TextBox txtMultiline = control as TextBox;
                txtMultiline.Multiline = true;
                control.Size = new System.Drawing.Size(250, 60);
 
            }


            if (Required)
            {
                Validator.ValidateEmpty(control);
            }
            if (!string.IsNullOrEmpty(ValidationRule))
            {
                Validator.Validate(control, ValidationRule, ErrorMessage);
            }
            else
            {
                switch (VariableType)
                {
                    case Constants.VariableType.Email:
                        Validator.ValidateEmail(control);
                        break;
                    case Constants.VariableType.Integer:
                        Validator.ValidateInteger(control);
                        break;
                    default:
                        break;

                }

            }



            return control;
        }

    }
}
